// $Id: RepositoryObjectDetailPage.java 54 2011-10-23 18:22:29Z huijzer@gmail.com $
package lime.browser.editor;

import lime.application.model.properties.ObjectProperty;
import lime.model.RepositoryObject;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.IDetailsPage;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;

public abstract class RepositoryObjectDetailPage implements IDetailsPage {

	private IManagedForm managedForm;
	private RepositoryObject input;
	FormToolkit toolkit;
	Composite client;
	private String sectionText; 

	public RepositoryObjectDetailPage(String sectionText) {
		this.sectionText = sectionText;
	}
	
	@Override
	public void initialize(IManagedForm managedForm) {
		this.managedForm = managedForm;
	}

	@Override
	public void dispose() {
		// Empty
	}

	@Override
	public boolean isDirty() {
		return false;
	}

	@Override
	public void commit(boolean onSave) {
		// Empty
	}

	@Override
	public boolean setFormInput(Object input) {
		return false;
	}

	@Override
	public void setFocus() {
		// Empty
	}

	@Override
	public boolean isStale() {
		return false;
	}

	@Override
	public void refresh() {
		update();
	}

	@Override
	public void selectionChanged(IFormPart part, ISelection selection) {
		IStructuredSelection structuredSelection = (IStructuredSelection) selection;
		if (structuredSelection.size() == 1) {
			input = (RepositoryObject) structuredSelection.getFirstElement();
		} else {
			input = null;
		}
		update();
	}

	@Override
	public void createContents(Composite parent) {

		TableWrapLayout layout = new TableWrapLayout();
		layout.topMargin = 5;
		layout.leftMargin = 5;
		layout.rightMargin = 2;
		layout.bottomMargin = 2;
		parent.setLayout(layout);

		toolkit = managedForm.getToolkit();
		
		Section section = toolkit.createSection(parent, Section.TITLE_BAR);
		section.marginWidth = 10;
		section.setText(sectionText);

		TableWrapData td = new TableWrapData(TableWrapData.FILL, TableWrapData.TOP);
		td.grabHorizontal = true;
		section.setLayoutData(td);
		
		client = toolkit.createComposite(section);
		GridLayout glayout = new GridLayout();
		glayout.marginWidth = 0;
		glayout.marginHeight = 0;
		glayout.numColumns = 2;
		client.setLayout(glayout);
		
		addTextWidgets();
		
		toolkit.paintBordersFor(section);
		section.setClient(client);
		
	}

	public RepositoryObject getInput() {
		return input;
	}
	
	public abstract void addTextWidgets();
	
	public Text addTextWidget(String label) {
		GridData gd = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
		gd.widthHint = 10;
		
		toolkit.createLabel(client, label);
		Text text = toolkit.createText(client, "", SWT.SINGLE);
		text.setEditable(false);
		text.setLayoutData(gd);
		
		return text;
	}

	public Text addTextWidget(ObjectProperty property) {
		
		GridData gd = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
		gd.widthHint = 10;
		
		Label label = toolkit.createLabel(client, property.label() + ": ");
		label.setToolTipText(property.description());
		
		Text text = toolkit.createText(client, "", SWT.SINGLE);
		text.setEditable(false);
		text.setLayoutData(gd);
		
		return text;
	}
	
	public abstract void update();
	
}
